﻿<UserControl x:Class="CannedHeater.View.GraphView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:charts="clr-namespace:Visiblox.Charts;assembly=Visiblox.Charts"
    xmlns:Converters="clr-namespace:CannedHeater.View.Converters"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <!-- DataContext set from StatisticsView -->
    
    <!-- IMPROVEMENT suggestion: Make it more general (we here expect temperatures and airflow) -->
    
    <UserControl.Resources>
        <Converters:TimeSpanConverter x:Key="timeSpanConverter" />
        <Converters:NumberToVisibilityConverter x:Key="dataCollectionVisibilityConverter" />
        <Converters:NumberToBooleanConverter x:Key="numberToBooleanConverter" />
    </UserControl.Resources>

    <StackPanel Orientation="Vertical" Background="Transparent">
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Temperatures" FontSize="30" />
                <TextBlock Text="{Binding TempUnit}" FontSize="30" />
                <TextBlock Text="{Binding TempRange, Converter={StaticResource timeSpanConverter}}" VerticalAlignment="Bottom" Margin="10,0,0,0"/>
            </StackPanel>

            <charts:Chart Height="250" Visibility="{Binding TempDataCollection.Count, Converter={StaticResource dataCollectionVisibilityConverter}, ConverterParameter=false}"
                    charts:AutoSeriesGenerator.XValuePath="Key"
                    charts:AutoSeriesGenerator.YValuePath="Value"
                    charts:AutoSeriesGenerator.TitlePath="Title"
                    charts:AutoSeriesGenerator.ChartSeriesProvider="LineSeries"
                    charts:AutoSeriesGenerator.ItemsSource="{Binding TempDataCollection}"
                    charts:AutoSeriesGenerator.ItemsSourcePath="Data">
                <charts:Chart.XAxis>
                    <charts:DateTimeAxis LabelsAutoFit="True" FontSize="12" />
                </charts:Chart.XAxis>
                <charts:Chart.YAxis>
                    <charts:LinearAxis />
                </charts:Chart.YAxis>
            </charts:Chart>

            <!-- Showing "Loading" box instead of graph when no data is available, notice the Visibility binding -->
            <Border BorderBrush="White" Height="250" BorderThickness="2"
                    Visibility="{Binding TempDataCollection.Count, Converter={StaticResource dataCollectionVisibilityConverter}, ConverterParameter=true}">
                <TextBlock Text="Loading..." FontSize="22" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Border>
            
            <!-- Buttons only enabled when graph is shown (not loading) -->
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Click="ShowLessTemp_Click" Content="Show less" FontSize="15" Padding="2"
                        IsEnabled="{Binding TempDataCollection.Count, Converter={StaticResource numberToBooleanConverter}}" />
                <Button Click="ShowMoreTemp_Click" Content="Show more" FontSize="15" Padding="2"
                        IsEnabled="{Binding TempDataCollection.Count, Converter={StaticResource numberToBooleanConverter}}" />
            </StackPanel>
        </StackPanel>

        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Air Flow" FontSize="30" />
                <TextBlock Text="{Binding AirFlowUnit}" FontSize="30" />
                <TextBlock Text="{Binding AirFlowRange, Converter={StaticResource timeSpanConverter}}" VerticalAlignment="Bottom" Margin="10,0,0,0"/>
            </StackPanel>

            <charts:Chart Height="250" Visibility="{Binding AirFlowDataCollection.Count, Converter={StaticResource dataCollectionVisibilityConverter}, ConverterParameter=false}"
                    charts:AutoSeriesGenerator.XValuePath="Key"
                    charts:AutoSeriesGenerator.YValuePath="Value"
                    charts:AutoSeriesGenerator.TitlePath="Title"
                    charts:AutoSeriesGenerator.ChartSeriesProvider="LineSeries"
                    charts:AutoSeriesGenerator.ItemsSource="{Binding AirFlowDataCollection}"
                    charts:AutoSeriesGenerator.ItemsSourcePath="Data">
                <charts:Chart.XAxis>
                    <charts:DateTimeAxis />
                </charts:Chart.XAxis>
                <charts:Chart.YAxis>
                    <charts:LinearAxis />
                </charts:Chart.YAxis>
            </charts:Chart>
            
            <!-- Showing "Loading" box instead of graph when no data is available, notice the Visibility binding -->
            <Border BorderBrush="White" Height="250" BorderThickness="2"
                    Visibility="{Binding AirFlowDataCollection.Count, Converter={StaticResource dataCollectionVisibilityConverter}, ConverterParameter=true}">
                <TextBlock Text="Loading..." FontSize="22" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Border>

            <!-- Buttons only enabled when graph is shown (not loading) -->
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Click="ShowLessAirFlow_Click" Content="Show less" FontSize="15" Padding="2"
                        IsEnabled="{Binding AirFlowDataCollection.Count, Converter={StaticResource numberToBooleanConverter}}" />
                <Button Click="ShowMoreAirFlow_Click" Content="Show more" FontSize="15" Padding="2"
                        IsEnabled="{Binding AirFlowDataCollection.Count, Converter={StaticResource numberToBooleanConverter}}" />
            </StackPanel>
        </StackPanel>
    </StackPanel>
</UserControl>
